define(['postImgCrop'],function(postImgCrop){
	function ImgCropTool(config){
		//targetBtn,cropSize,cropType
		var imgCropTool=this;
		imgCropTool.cropSize=config.cropSize;
		imgCropTool.cropScale=imgCropTool.cropSize[0]/imgCropTool.cropSize[1];
		imgCropTool.$dom=$('<div class="imgUploadTool"></div>');
		imgCropTool.callback={
			success:config.success||function(){}
		}
		function ImgCropBox(){
			var imgCropBox=this;
			imgCropBox.imgSrc='img/img_avatar_upload.png';
			imgCropBox.getImgSrc=function(){
				var defaultSrc='img/img_avatar_upload.png';
				if(imgCropBox.imgSrc===defaultSrc){
					return false;
				}else{
					return imgCropBox.imgSrc
				}
			};
			imgCropBox.$dom=$('<div class="imgCropBox"></div>');

			imgCropBox.$cropBoardBox=$('<div class="cropBoardBox"></div>').appendTo(imgCropBox.$dom);
			imgCropBox.$cropBoard=$('<div class="cropBoard"></div>').appendTo(imgCropBox.$cropBoardBox);
			imgCropBox.$cropPreviewer=$('<div class="cropPreviewer"></div>').appendTo(imgCropBox.$dom);

			function ImgPreviewBox(){
				var imgPreviewBox=this;
				imgPreviewBox.$dom=$('<div class="viewer"></div>');
				imgPreviewBox.$title=$('<div class="title">预览</div>').appendTo(imgPreviewBox.$dom);
				imgPreviewBox.$imgBox=$('<div class="imgBox" style="width:'+imgCropTool.cropSize[0]+'px;height:'+imgCropTool.cropSize[1]+'px;"></div>').appendTo(imgPreviewBox.$dom);
				imgPreviewBox.$img=$('<img src="'+imgCropBox.imgSrc+'" />').appendTo(imgPreviewBox.$imgBox);
				imgPreviewBox.update=function(imgSrc){
					imgPreviewBox.$img.attr('src',imgSrc);
				}
			}
			imgCropBox.imgPreviewBox=new ImgPreviewBox();
			imgCropBox.imgPreviewBox.$dom.appendTo(imgCropBox.$cropPreviewer);

			function cropImgReloadJCrop(){
				require(['js/assets/jquery.jcrop','js/assets/css!js/assets/css/jquery.jcrop.css'], function (){
					imgCropBox.$cropImg=imgCropBox.$cropBoard.children('img');

					imgCropBox.$cropImg.Jcrop(
						{
							allowSelect:false,
							aspectRatio:(imgCropTool.cropScale || 0),//比例
							createHandles:['nw','ne','se','sw'],//设置边角控制器
							createDragbars:[],//设置边框控制器
							boxWidth:400,
							boxHeight:300,
							onChange:function(){
								var imgBounds=this.getBounds();
								imgBounds={
									width:imgBounds[0],
									height:imgBounds[1]
								}
								var cropPosition=this.tellSelect()
								function artWorkThum(thumSize){
									var positionObj={
										width:0,
										height:0,
										left:0,
										top:0,
										scale:(cropPosition.x2-cropPosition.x)/thumSize
									}
									positionObj.width=Math.floor(imgBounds.width/positionObj.scale);
									positionObj.height=Math.floor(imgBounds.height/positionObj.scale);
									positionObj.left=-Math.floor(cropPosition.x/positionObj.scale);
									positionObj.top=-Math.floor(cropPosition.y/positionObj.scale);

									return positionObj;
								}
								if(imgCropTool.cropScale){
									imgCropBox.imgPreviewBox.$img.css(artWorkThum(imgCropTool.cropSize[0]));
								}

								imgCropBox.cropPosition=cropPosition;
							}
						},
						function(){
							var jcrop_api=this;
							var imgBounds=jcrop_api.getBounds();
							imgBounds={
								width:imgBounds[0],
								height:imgBounds[1]
							}
							var x,y,x2,y2,w_max,h_max;
							if(imgBounds.width/imgBounds.height>imgCropTool.cropScale){
								h_max=imgBounds.height;
								w_max=h_max*imgCropTool.cropScale;
							}else{
								w_max=imgBounds.width;
								h_max=w_max/imgCropTool.cropScale;
							}
							x=(imgBounds.width-w_max)/2;
							y=(imgBounds.height-h_max)/2;
							x2=x+w_max;
							y2=y+h_max;

							if(imgBounds.width/imgBounds.height>=400/300){
								var img_bw=400;
								var img_bh=img_bw/(imgBounds.width/imgBounds.height);
								imgCropBox.$cropBoard.addClass('crop').css({
									'padding':(300-img_bh)/2+'px 0 0 0'
								});
							}else{
								var img_bh=300;
								var img_bw=img_bh*(imgBounds.width/imgBounds.height);
								imgCropBox.$cropBoard.addClass('crop').css({
									'padding':'0 0 0 '+(400-img_bw)/2+'px'
								});
							}
							if(!imgCropTool.cropScale){
								x=0;
								y=0;
								x2=imgBounds.width;
								y2=imgBounds.height;
							}
							jcrop_api.setSelect([x,y,x2,y2]);
						}
					);

				});
			}

			imgCropBox.addImg=function (newImgSrc){
				imgCropBox.imgSrc=newImgSrc;
				imgCropBox.cropPosition=false;
				imgCropBox.$cropBoard.html($('<img class="artworkAvatar" style="display:none;" />').attr('src',imgCropBox.imgSrc));
				imgCropBox.imgPreviewBox.update(imgCropBox.imgSrc);
				cropImgReloadJCrop();
			}
		}
		imgCropTool.imgCropBox=new ImgCropBox();
		imgCropTool.imgCropBox.$dom.appendTo(imgCropTool.$dom);
		imgCropTool.addImg=imgCropTool.imgCropBox.addImg;
		imgCropTool.addImg(config.imgSrc)



		imgCropTool.myPopUpBox=new PopUpBox({
			'type':'dom',
			'style':'default',
			'show':true,
			'submitBtnType':'alert',
			//'bgShow':false,
			'titleText':'图片上传',
			'content':imgCropTool.$dom,
			'submitBtnText':{
				'yes':'保存'
			},
			'callback':{
				'yes':function(){
					var cropImgSrc=imgCropTool.imgCropBox.getImgSrc();
					var cropImgPosition=imgCropTool.imgCropBox.cropPosition;
					if(!cropImgSrc){
						_Tips('请上传图片')
						return false;
					}
					if(!cropImgPosition){
						_Tips('请选择裁剪区域')
						return false;
					}

					var cropAjaxStatus=false;
					postImgCrop(
						function(cropedImgSrc){
							config.success(cropedImgSrc);
							cropAjaxStatus=true;
						},
						cropImgSrc,
						cropImgPosition,
						imgCropTool.cropSize[0]+'x'+imgCropTool.cropSize[1]
					);
					return cropAjaxStatus;
				}
			},
			'position':{
				//width:640
				//height:600
			}
		});
	}
	window.ImgCropTool=ImgCropTool;
	return ImgCropTool;
})
